﻿using AiChiTu.TraceabilitySystem.Domain.AiChiTuManagement;
using AiChiTu.TraceabilitySystem.ErrorCode;
using AiChiTu.TraceabilitySystem.ErrorCode.Extensions;
using AiChiTu.TraceabilitySystem.Infrastructure.Interfaces;
using AiChiTu.TraceabilitySystem.Read.API.Applications.Commands.ProductNames;
using MediatR;
using System.Collections.Generic;

namespace AiChiTu.TraceabilitySystem.Read.API.Applications.CommandsHandlers.ProductNames
{
	public class QueryAllProductNameCommandHandle : IRequestHandler<QueryAllProductNameCommand, APIResult<List<ProductName>>>
	{
		private readonly IBaseRepository<ProductName> productnameRepository;

		public QueryAllProductNameCommandHandle(IBaseRepository<ProductName> productnameRepository)
		{
			this.productnameRepository = productnameRepository;
		}
		/// <summary>
		/// 查询全部商品名称请求命令处理
		/// </summary>
		/// <param name="request">查询全部商品名称请求命令</param>
		/// <param name="cancellationToken">取消</param>
		/// <returns>返回任务</returns>
		public async Task<APIResult<List<ProductName>>> Handle(QueryAllProductNameCommand request, CancellationToken cancellationToken)
		{
			APIResult<List<ProductName>> result = new APIResult<List<ProductName>>();
			result.Code = ResultCode.QuerySuccess;
			result.Message = ResultCode.QuerySuccess.GetDescription();

			List<ProductName> list = productnameRepository.QueryAllAsync().ToList();
			result.Data = list;

			return await Task.FromResult(result);
		}
	}
}
